package com.web.portal.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.Lab;
import com.web.portal.exception.PortalException;
import com.web.portal.service.LabService;

@Controller
public class LabController {
	@Autowired
	LabService labService;

	public LabService getLabService() {
		return labService;
	}

	public void setLabService(LabService labService) {
		this.labService = labService;
	}

	@RequestMapping(value = "/admin/getlabinfo.do", method = RequestMethod.GET)
	public String getLabInfo(Map<String, Object> model) throws PortalException {
		Lab lab = new Lab();
		List<Lab> labInfos = labService.getList(lab);
		Lab labs=null;
		if(!labInfos.isEmpty()){
			labs=labInfos.get(0);
		}
		List<Lab>labInfo= new ArrayList<Lab>();
		labInfo.add(labs);
		model.put("labs", labInfo);
		return "lab_view";

	}

	@RequestMapping(value = "/admin/getlabmanager.do", method = RequestMethod.GET)
	public String getLabManagerPage(Map<String, Object> model)  {

		return "lab";

	}

	@RequestMapping(value = "/admin/getlabinfoform.do", method = RequestMethod.GET)
	public String getLabInfoForm(Model model) throws PortalException {
		model.addAttribute(new Lab());
		return "add_lab_info";
	}

	@RequestMapping(value = "/admin/filllabinfoform.do", method = RequestMethod.POST)
	public String addLabInfo(@Valid @ModelAttribute("lab") final Lab lab,
			BindingResult result, Model model) {
		if (result.hasErrors()) {

			return "redirect:/admin/getlabinfoform.do";

		}
		/*
		 * save lab
		 */
		labService.createOrUpdate(lab);
		return "redirect:/admin/getlabinfo.do";
	}

	@RequestMapping(value = "/admin/getlabeditpage.do", method = RequestMethod.GET)
	public String getLabEditPage(@RequestParam("id") final String id,
			Map model, HttpServletRequest request) throws PortalException {
		if (!(id == null)) {
			request.getSession().setAttribute("id", id);
			Lab lab = labService.getLab(id);
			model.put("lab", lab);
			return "edit_lab_info";
		}
		return "redirect:/admin/getlabinfo.do";
	}

	@RequestMapping(value = "/admin/editlab.do", method = RequestMethod.POST)
	public String EditLab(@Valid @ModelAttribute("lab") final Lab lab,
			BindingResult result, HttpServletRequest request) {
		if (!(lab.getId() == null)) {
			Lab editedLab = labService.getLab(lab.getId());

			/*
			 * update the info
			 */
			editedLab.setId(lab.getId());
			editedLab.setTerm(lab.getTerm());
			editedLab.setOpenDate(lab.getOpenDate());
			editedLab.setEndDate(lab.getEndDate());
			editedLab.setHours(lab.getHours());
			editedLab.setLocation(lab.getLocation());
			editedLab.setPhone(lab.getPhone());
			editedLab.setHolidays(lab.getHolidays());
			/*
			 * save it
			 */
			labService.createOrUpdate(editedLab);

			return "redirect:/admin/getlabinfo.do";
		} else {
			return "redirect:/admin/getlabinfo.do";
		}
	}

}
